package com.jpyl.jpMedicalTreatment.view.widget;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jpyl.jpMedicalTreatment.R;

/**
 * Created by dongbinbin on 2015/9/14.
 */
public class LogOutDialog extends Dialog {

    public LogOutDialog(Context context, int theme) {
        super(context, theme);
    }

    public LogOutDialog(Context context) {
        super(context);
    }

    public static class Builder {

        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View contentView;
        private DialogInterface.OnClickListener positiveButtonClickListener, negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setContentView(View v) {
            this.contentView = v;
            return this;
        }

        public Builder setPositiveButton(int positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText, DialogInterface.OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText, DialogInterface.OnClickListener listener) {
            this.negativeButtonText = (String) context.getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText, DialogInterface.OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        @SuppressLint("InflateParams")
        public LogOutDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final LogOutDialog dialog = new LogOutDialog(context, R.style.MedicalDialog);
            View layout = inflater.inflate(R.layout.dialog_logout_layout, null);
            if (title != null && !title.equals("")) {
                ((TextView) layout.findViewById(R.id.dialog_title)).setText("" + title);
            }
            if (message != null && !message.equals("")) {
                ((TextView) layout.findViewById(R.id.dialog_message)).setText("" + message);
            } else {
                ((TextView) layout.findViewById(R.id.dialog_message)).setVisibility(View.GONE);
            }
            // POSITIVE BUTTON==================================================
            if (positiveButtonText != null) {
                ((TextView) layout.findViewById(R.id.dialog_make_sure_btn)).setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((RelativeLayout) layout.findViewById(R.id.dialog_make_sure)).setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                } else {
                    layout.findViewById(R.id.dialog_make_sure).setVisibility(View.GONE);
                }
            } else {
                layout.findViewById(R.id.dialog_make_sure).setVisibility(View.GONE);
            }
            // NEGATIVE BUTTON==================================================
            if (negativeButtonText != null) {
                ((TextView) layout.findViewById(R.id.dialog_cancel_btn)).setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((RelativeLayout) layout.findViewById(R.id.dialog_cancel)).setOnClickListener(new View.OnClickListener() {

                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                } else {
                    layout.findViewById(R.id.dialog_cancel).setVisibility(View.GONE);
                }
            } else {
                layout.findViewById(R.id.dialog_cancel).setVisibility(View.GONE);
            }
            dialog.setContentView(layout);
            return dialog;
        }

        /**
         * @return the title
         */
        public String getTitle() {
            return title;
        }

        /**
         * @return the contentView
         */
        public View getContentView() {
            return contentView;
        }
    }
}
